#Turnbull-Dugarte & López-Ortega (2025)

#---- Replication script #1 ----

#install.packages("pacman")
library(pacman)

#remotes::install_github("leeper/cregg") If installing of cregg required

p_load(here, tidyverse, haven, modelsummary, jtools, cregg,
       list, ggpubr, survey, xtable, interactions, viridisLite,
       ggdist, patchwork, estimatr, margins, grf, starbility, miceadds)
theme_set(theme_ggdist())
col2 <- c("grey50", "grey10")



##LOADING DATA AND WRANGLING##
directES <- read_dta("data/ES_observational.dta")

directES$degree <- ifelse(directES$educ >= 4, "Yes", "No")
directES$derecha_num <- as.integer(directES$party_id %in% c("0", "2") |
                                     (directES$party_id == "8888" & directES$party_id_squeeze %in% c("0", "2")))
directES$derecha<- factor(directES$derecha_num,
                          levels = c(0, 1),
                          labels = c("Left-wing", "Right-wing"))

directES <- directES %>%
  mutate(party_id = case_when(
    party_id == 0 ~ "PP",
    party_id == 1 ~ "PSOE",
    party_id == 2 ~ "VOX",
    party_id == 3 ~ "Sumar",
    party_id == 4 ~ "Unidas Podemos",
    party_id %in% 6:45 ~ "Other",
    party_id == 9999 ~ "None",
    TRUE ~ as.factor(party_id)))
table(directES$party_id)

directES <- directES %>%
  mutate(party_id_squeeze = case_when(
    party_id_squeeze == 0 ~ "PP",
    party_id_squeeze == 1 ~ "PSOE",
    party_id_squeeze == 2 ~ "VOX",
    party_id_squeeze == 3 ~ "Sumar",
    party_id_squeeze == 4 ~ "Unidas Podemos",
    party_id_squeeze %in% 5:9 ~ "Other",
    TRUE ~ as.factor(party_id_squeeze)))
table(directES$party_id_squeeze)

directES$partyID_full <- ifelse(directES$party_id == "None", 
                                directES$party_id_squeeze, 
                                directES$party_id)
table(directES$partyID_full)


directES <- directES %>%
  mutate(party_distance = case_when(
    party_distance == 0 ~ "PP",
    party_distance == 1 ~ "PSOE",
    party_distance == 2 ~ "VOX",
    party_distance == 3 ~ "Sumar",
    party_distance == 4 ~ "Unidas Podemos",
    party_distance == 5 ~ "Other",
    party_distance == 6 ~ "Other",
    party_distance == 7 ~ "Other",
    party_distance == 8 ~ "Other",
    party_distance == 9 ~ "Other",
    party_distance == 8888 ~ "None",
    TRUE ~ as.factor(party_distance)))


directES <- directES %>%
  mutate(distance_squeeze = case_when(
    distance_squeeze == 0 ~ "PP",
    distance_squeeze == 1 ~ "PSOE",
    distance_squeeze == 2 ~ "VOX",
    distance_squeeze == 3 ~ "Sumar",
    distance_squeeze == 4 ~ "Unidas Podemos",
    distance_squeeze == 5 ~ "Other",
    distance_squeeze == 6 ~ "Other",
    distance_squeeze == 7 ~ "Other",
    distance_squeeze == 8 ~ "Other",
    distance_squeeze == 9 ~ "Other",
    TRUE ~ as.factor(party_distance)))


directES$distance_full <- ifelse(directES$party_distance == "None", 
                                 directES$distance_squeeze, 
                                 directES$party_distance)


table(directES$distance_full)
directES$VOX_mostdistant <- ifelse(directES$distance_full == "VOX", 1, 0)
table(directES$VOX_mostdistant, directES$partyID_full)

directES <- directES %>%
  mutate_at(vars(starts_with("hud_")), ~ifelse(. == 9999, NA, .))
directES <- directES %>%
  mutate_at(vars(starts_with("hud_")), ~ifelse(. == 1, 4, ifelse(. == 2, 3, ifelse(. == 3, 2, ifelse(. == 4, 1, NA)))))


directES<- directES %>%
  mutate(hud_ingroup_1 = ifelse(is.na(hud_ingroup_1), hud_ingroup_squeeze_1, hud_ingroup_1))
directES<- directES %>%
  mutate(hud_ingroup_2 = ifelse(is.na(hud_ingroup_2), hud_ingroup_squeeze_2, hud_ingroup_2))
directES<- directES %>%
  mutate(hud_ingroup_3 = ifelse(is.na(hud_ingroup_3), hud_ingroup_squeeze_3, hud_ingroup_3))
directES<- directES %>%
  mutate(hud_ingroup_4 = ifelse(is.na(hud_ingroup_4), hud_ingroup_squeeze_4, hud_ingroup_4))

directES<- directES %>%
  mutate(hud_outgroup_1 = ifelse(is.na(hud_outgroup_1), hud_outgroup_squeez_1, hud_outgroup_1))
directES<- directES %>%
  mutate(hud_outgroup_2 = ifelse(is.na(hud_outgroup_2), hud_outgroup_squeez_2, hud_outgroup_2))
directES<- directES %>%
  mutate(hud_outgroup_3 = ifelse(is.na(hud_outgroup_3), hud_outgroup_squeez_3, hud_outgroup_3))
directES<- directES %>%
  mutate(hud_outgroup_4 = ifelse(is.na(hud_outgroup_4), hud_outgroup_squeez_4, hud_outgroup_4))

directES$ingroup_affect <- (directES$hud_ingroup_1 + directES$hud_ingroup_2 +
                              directES$hud_ingroup_3 + directES$hud_ingroup_4) / 4

directES$outgroup_affect <- (directES$hud_outgroup_1 + directES$hud_outgroup_2 +
                               directES$hud_outgroup_3 + directES$hud_outgroup_4) / 4


directUK <- read_dta("data/UK_observational.dta")

directUK$derecha_num <- as.integer(directUK$party_id %in% c("0", "6") |
                                     (directUK$party_id == "8888" & directUK$party_id_squeeze %in% c("1", "7")))
directUK$derecha<- factor(directUK$derecha_num,
                          levels = c(0, 1),
                          labels = c("Left-wing", "Right-wing"))
directUK <- directUK %>%
  mutate(party_id = case_when(
    party_id == 0 ~ "Conservative party",
    party_id == 1 ~ "Labour party",
    party_id == 2 ~ "Liberal Democrats",
    party_id == 3 ~ "The Green party",
    party_id == 4 ~ "SNP",
    party_id == 5 ~ "Plaid Cymru",
    party_id == 6 ~ "Reform UK",
    party_id == 8888 ~ "None",
    TRUE ~ as.factor(party_id)))

directUK <- directUK %>%
  mutate(party_id_squeeze = case_when(
    party_id_squeeze == 1 ~ "Conservative party",
    party_id_squeeze == 2 ~ "Labour party",
    party_id_squeeze == 3 ~ "Liberal Democrats",
    party_id_squeeze == 4 ~ "The Green party",
    party_id_squeeze == 5 ~ "SNP",
    party_id_squeeze == 6 ~ "Plaid Cymru",
    party_id_squeeze == 7 ~ "Reform UK",
    TRUE ~ as.factor(party_id_squeeze)))

directUK$partyID_full <- ifelse(directUK$party_id == "None", 
                                directUK$party_id_squeeze, 
                                directUK$party_id)
table(directUK$partyID_full)

directUK <- directUK %>%
  mutate(party_distance = case_when(
    party_distance == 0 ~ "Conservative party",
    party_distance == 1 ~ "Labour party",
    party_distance == 2 ~ "Liberal Democrats",
    party_distance == 3 ~ "The Green party",
    party_distance == 4 ~ "SNP",
    party_distance == 5 ~ "Plaid Cymru",
    party_distance == 6 ~ "Reform UK",
    party_distance == 8888 ~ "None",
    party_distance == 9999 ~ "None",
    TRUE ~ as.factor(party_distance)))


directUK <- directUK %>%
  mutate(distance_squeeze = case_when(
    distance_squeeze == 0 ~ "Conservative party",
    distance_squeeze == 1 ~ "Labour party",
    distance_squeeze == 2 ~ "Liberal Democrats",
    distance_squeeze == 3 ~ "The Green party",
    distance_squeeze == 4 ~ "SNP",
    distance_squeeze == 5 ~ "Plaid Cymru",
    distance_squeeze == 6 ~ "Reform UK",
    TRUE ~ as.factor(party_distance)))


directUK$distance_full <- ifelse(directUK$party_distance == "None", 
                                 directUK$distance_squeeze, 
                                 directUK$party_distance)


table(directUK$distance_full)
directUK$reform_mostdistant <- ifelse(directUK$distance_full == "Reform UK", 1, 0)
table(directUK$reform_mostdistant, directUK$partyID_full)

directUK <- directUK %>%
  mutate_at(vars(starts_with("hud_")), ~ifelse(. == 9999, NA, .))
directUK <- directUK %>%
  mutate_at(vars(starts_with("hud_")), ~ifelse(. == 1, 4, ifelse(. == 2, 3, ifelse(. == 3, 2, ifelse(. == 4, 1, NA)))))


directUK<- directUK %>%
  mutate(hud_ingroup_1 = ifelse(is.na(hud_ingroup_1), hud_ingroup_squeeze_1, hud_ingroup_1))
directUK<- directUK %>%
  mutate(hud_ingroup_2 = ifelse(is.na(hud_ingroup_2), hud_ingroup_squeeze_2, hud_ingroup_2))
directUK<- directUK %>%
  mutate(hud_ingroup_3 = ifelse(is.na(hud_ingroup_3), hud_ingroup_squeeze_3, hud_ingroup_3))
directUK<- directUK %>%
  mutate(hud_ingroup_4 = ifelse(is.na(hud_ingroup_4), hud_ingroup_squeeze_4, hud_ingroup_4))

directUK<- directUK %>%
  mutate(hud_outgroup_1 = ifelse(is.na(hud_outgroup_1), hud_outgroup_squeez_1, hud_outgroup_1))
directUK<- directUK %>%
  mutate(hud_outgroup_2 = ifelse(is.na(hud_outgroup_2), hud_outgroup_squeez_2, hud_outgroup_2))
directUK<- directUK %>%
  mutate(hud_outgroup_3 = ifelse(is.na(hud_outgroup_3), hud_outgroup_squeez_3, hud_outgroup_3))
directUK<- directUK %>%
  mutate(hud_outgroup_4 = ifelse(is.na(hud_outgroup_4), hud_outgroup_squeez_4, hud_outgroup_4))

directUK$ingroup_affect <- (directUK$hud_ingroup_1 + directUK$hud_ingroup_2 +
                              directUK$hud_ingroup_3 + directUK$hud_ingroup_4) / 4

directUK$outgroup_affect <- (directUK$hud_outgroup_1 + directUK$hud_outgroup_2 +
                               directUK$hud_outgroup_3 + directUK$hud_outgroup_4) / 4


tinder_conjoint_UK <- readRDS("data/conjoint_UK.rds")
table(tinder_conjoint_UK$derecha_num)

tinder_conjoint_UK <-tinder_conjoint_UK%>% 
  mutate(BI= as.factor(BISEXUAL),
         TRANS= as.factor(TRANS),
         CONGRUENCE= as.factor(CONGRUENCE),
         FLAG= as.factor(NATIONALITY),
         DISTANCE= as.factor(DISTANCE),
         RELATIONSHIP= as.factor(RELATIONSHIP),
         PROS= as.factor(PROS),
         resp_gender=as.factor(gender),
         PARTY=as.factor(PARTY),
         experimental_group=as.factor(experimental_group),
         EDUCATION_SLIM= as.factor(EDUCATION_SLIM),
         JOB_SLIM= as.factor(JOB_SLIM),
         ETHNICITY= as.factor(ETHNICITY),
         EMOTION= as.factor(EMOTION),
         partyID_full= as.factor(partyID_full),
         outgroup_hate= as.factor(outgroup_hate))


tinder_conjoint_UK$BI<- factor(tinder_conjoint_UK$BI,
                               levels = c("FALSE","TRUE"),
                               labels = c("Hetero.", "Bi."))

tinder_conjoint_UK$TRANS<- factor(tinder_conjoint_UK$TRANS,
                                  levels = c("FALSE","TRUE"),
                                  labels = c("Cis.", "Trans."))

tinder_conjoint_UK$CONGRUENCE<- factor(tinder_conjoint_UK$CONGRUENCE,
                                       levels = c("no","yes"),
                                       labels = c("Less gender congruent", "More gender congruent"))


tinder_conjoint_UK$FLAG<- factor(tinder_conjoint_UK$FLAG,
                                 levels = c("No flag", "UK flag", "UK-EU flag", "English flag", "English-EU flag", "EU flag"))


tinder_conjoint_UK$derecha<- factor(tinder_conjoint_UK$derecha_num,
                                    levels = c(0, 1),
                                    labels = c("Left-wing", "Right-wing"))

tinder_conjoint_UK <- tinder_conjoint_UK |> 
  mutate(derecha_mujer= factor(case_when(derecha== "Right-wing" & gender=="a woman" ~ "Right-wing woman",
                                         derecha== "Right-wing" & gender=="a man" ~ "Right-wing man",
                                         derecha== "Left-wing" & gender=="a woman" ~ "Left-wing woman",
                                         derecha== "Left-wing" & gender=="a man" ~ "Left-wing man",
  )))

tinder_conjoint_UK <- tinder_conjoint_UK |> 
  mutate(queer= factor(case_when(sex_id== "0" ~ "Heterosexual",
                                 sex_id== "1" | sex_id== "2" |sex_id== "3" |sex_id== "4" ~ "LGBTQ+"
  )))



tinder_conjoint_UK$PARTY<- factor(tinder_conjoint_UK$PARTY,
                                  levels = c("#VoteReformUK","#VoteLibDem", "#VoteLabour", "#VoteConservative", "#VoteGreens"),
                                  labels = c("Reform UK", "Lib Dem", "Labour", "Conservatives", "Greens"))
tinder_conjoint_UK$PARTY <- relevel(tinder_conjoint_UK$PARTY, ref = "Conservatives")


tinder_conjoint_UK$derecha_hate <- ifelse(!is.na(tinder_conjoint_UK$outgroup_hate),
                                          paste(tinder_conjoint_UK$outgroup_hate, tinder_conjoint_UK$derecha),
                                          NA)

tinder_conjoint_UK <-tinder_conjoint_UK%>% 
  mutate(derecha_hate= as.factor(derecha_hate))

tinder_conjoint_UK <- subset(tinder_conjoint_UK, experimental_group!="Without party labels") 
tinder_conjoint_UK_noReform <- subset(tinder_conjoint_UK, partyID_full != "Reform UK")
table(tinder_conjoint_UK_noReform$partyID_full)

tinder_conjoint_ES <- readRDS("data/conjoint_ES.rds")

table(tinder_conjoint_ES$derecha_num)
table(tinder_conjoint_ES$gender)
table(tinder_conjoint_ES$educ)

tinder_conjoint_ES$degree <- ifelse(tinder_conjoint_ES$educ >= 4, "Yes", "No")
table(tinder_conjoint_ES$degree)
table(tinder_conjoint_ES$Age)

tinder_conjoint_ES <-tinder_conjoint_ES%>% 
  mutate(BI= as.factor(BISEXUAL),
         TRANS= as.factor(TRANS),
         CONGRUENCE= as.factor(CONGRUENCE),
         FLAG= as.factor(NATIONALITY),
         DISTANCE= as.factor(DISTANCE),
         RELATIONSHIP= as.factor(RELATIONSHIP),
         VEG= as.factor(VEG),
         resp_gender=as.factor(gender),
         PARTY=as.factor(PARTY),
         experimental_group=as.factor(experimental_group),
         EDUCATION_SLIM= as.factor(EDUCATION_SLIM),
         JOB_SLIM= as.factor(JOB_SLIM),
         ETHNICITY= as.factor(ETHNICITY),
         EMOTION= as.factor(EMOTION),
         partyID_full= as.factor(partyID_full),
         outgroup_hate= as.factor(outgroup_hate))

tinder_conjoint_ES$BI<- factor(tinder_conjoint_ES$BI,
                               levels = c("FALSE","TRUE"),
                               labels = c("Hetero.", "Bi."))

tinder_conjoint_ES$TRANS<- factor(tinder_conjoint_ES$TRANS,
                                  levels = c("FALSE","TRUE"),
                                  labels = c("Cis.", "Trans."))

tinder_conjoint_ES$derecha<- factor(tinder_conjoint_ES$derecha_num,
                                    levels = c(0, 1),
                                    labels = c("Left-wing", "Right-wing"))

tinder_conjoint_ES$CONGRUENCE<- factor(tinder_conjoint_ES$CONGRUENCE,
                                       levels = c("no","yes"),
                                       labels = c("Less gender congruent", "More gender congruent"))

table(tinder_conjoint_ES$derecha, tinder_conjoint_ES$party_id)
tinder_conjoint_ES <- tinder_conjoint_ES |> 
  mutate(derecha_mujer= factor(case_when(derecha== "Right-wing" & gender=="a woman" ~ "Right-wing woman",
                                         derecha== "Right-wing" & gender=="a man" ~ "Right-wing man",
                                         derecha== "Left-wing" & gender=="a woman" ~ "Left-wing woman",
                                         derecha== "Left-wing" & gender=="a man" ~ "Left-wing man",
  )))

tinder_conjoint_ES <- tinder_conjoint_ES |> 
  mutate(queer= factor(case_when(sex_id== "0" ~ "Heterosexual",
                                 sex_id== "1" | sex_id== "2" |sex_id== "3" |sex_id== "4" ~ "LGBTQ+"
  )))


table(tinder_conjoint_ES$PARTY)
tinder_conjoint_ES$PARTY<- factor(tinder_conjoint_ES$PARTY,
                                  levels = c("#VotaPP","#VotaPSOE", "#VotaSumar", "#VotaVox"),
                                  labels = c("PP", "PSOE", "Sumar", "VOX"))
tinder_conjoint_ES$PARTY <- relevel(tinder_conjoint_ES$PARTY, ref = "PP")

tinder_conjoint_ES <- subset(tinder_conjoint_ES, experimental_group!="Without party labels") 
tinder_conjoint_ES_noVOX <- subset(tinder_conjoint_ES, partyID_full != "VOX")

